import { Component, ViewChild } from '@angular/core';
import { IonicPage, NavController, ViewController } from 'ionic-angular';
import { HttpProvider } from '../../providers/http/http';
import { StorageProvider } from '../../providers/storage/storage';
import { DialogProvider } from '../../providers/dialog/dialog';
import { DomSanitizer } from '@angular/platform-browser';
import { QuestionDataService } from '../../providers/service/question-data.service';
import { Network } from '@ionic-native/network';
/**
 * Generated class for the QuestionAndAnswerPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-question-and-answer',
  templateUrl: 'question-and-answer.html',
})
export class QuestionAndAnswerPage {

	//  这是全局变量 常见问答
	 questionanswer :Questionanswer[] =new Array<Questionanswer>();
	
	

	// 页数
	page = 1;
	//有网状态=1 断网=0
	Internet = 1;

	constructor(
		public navCtrl: NavController,
		public viewCtrl: ViewController,
		public http: HttpProvider,
		public storage: StorageProvider,
		public dialog: DialogProvider,
		public question: QuestionDataService,
		private network: Network,
		private sanitizer: DomSanitizer) {}

  async ionViewDidLoad() {
	console.log('ionViewDidLoad QuestionAndAnswerPage');
	if (this.network.type!=='none' && this.network.type !== '2g') {
		this.Internet = 1;
		this.getQuestion();
	    return;
	}
	if (this.network.type === 'none' || this.network.type === '2g') {
		this.Internet = 0;
		let a = await this.question.find((questionanswer) => questionanswer.key !== '');
		this.questionanswer = a;
		return;
	}
  }

  assembleHTML(strHTML:any) {
    return this.sanitizer.bypassSecurityTrustHtml(strHTML);
    }

// //  获取常见问答方法
// 	getQuestion() {
// 		this.http.postJson('', 'system/softwen/index', {
// 			'distinct' : 1,
// 			'where' :["=","soft_wen_type",4],
// 			'orderBy' : "created_at asc"
// 		}, '').then(res => {
// //			console.log(res)
// 			if(res['status'] == 200) {
// 				this.getdata (res['data']);
// 			}
// 		});
// 		console.log(this.data);
// 	}

	 //获取常见问答方法
	getQuestion() {
		this.http.postJson('', 'system/softwen/getsoftwen', {
			"tables":"5",               
			"file_type_id":"8",       
			"soft_wen_type":"4",
			"limit":"10",
			"page": this.page
		}, '').then(res => {
			console.log(res)
			if(res['status'] == 200) {
				this.getdata (res['data']);
			}
		});;
		
	}

//  遍历数据
    async getdata(questionanswer) {
		this.questionanswer.splice(0);
		console.log(questionanswer)
		for (let message of questionanswer) {
			if (message) {
				var b = {
					/*根据提供的接口地址传参数,配置文件在http.ts里查看,里面写的是链接服务器的配置*/	
				    titles : message.titles,
            		summary: message.summary,
            		description: message.description,
            		
				};
				await this.questionanswer.push(b);
			}
		}//this.data.reverse();把数组中的元素倒序排列
		this.question.deleteAll();
		for(var i=0 ; i<this.questionanswer.length; i++){
			this.question.save(this.questionanswer[i]);
		}
		console.log(this.questionanswer);
	}

	//无限滚动(下滑加载)
	async lodeMessage(infiniteScroll) {
		
		this.page++;
		console.log("11111="+this.page)
		const res = await this.http.postJson('', 'system/softwen/getsoftwen',
		  {
			"tables":"5",               
			"file_type_id":"8",       
			"soft_wen_type":"4",
			"limit":"10",
			"page":this.page,
		  }, '').then(res => {
			if (res['status'] == 200) {
				for (let message of res['data']) {
					if (message) {
						var b = {
							/*根据提供的接口地址传参数,配置文件在http.ts里查看,里面写的是链接服务器的配置*/	
							titles : message.titles,
							summary: message.summary,
							description: message.description,
							
						};
						/*
						splice() 方法可删除从 index 处开始的零个或多个元素，并且用参数列表中声明的一个或多个值来替换那些被删除的元素。
						如果从 arrayObject 中删除了元素，则返回的是含有被删除的元素的数组。
						arrayObject.splice(index,howmany,item1,.....,itemX)
						index	必需。整数，规定添加/删除项目的位置，使用负数可从数组结尾处规定位置。
						howmany	必需。要删除的项目数量。如果设置为 0，则不会删除项目。
						item1, ..., itemX	可选。向数组添加的新项目。
						this.data.splice(0,0,b); 从第一个元素开始，删除0条
						
						*/
				
						this.questionanswer.push(b);
					}
				}
			  setInterval(infiniteScroll.complete(), 500);
			}
		  });
	  }
}

export class Questionanswer{
	titles:any;
	description:any;
}
